//
//  HGPopView.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/8/7.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGPopView.h"

@implementation HGPopView {
    UITextField *jfTextField;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        //初始化子视图
        [self initSubview];
        
    }
    return self;
}



- (void)initSubview {

    CGFloat margin = 10;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"兑换惊喜";
    [self addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.offset(margin);
    }];
    
    UIButton *disBtn = [[UIButton alloc] init];
    [disBtn setImage:[UIImage imageNamed:@"dismiss"] forState:(UIControlStateNormal)];
    [disBtn addTarget:self action:@selector(closeButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:disBtn];
    
    [disBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLabel);
        make.top.equalTo(self);
        make.right.offset(-margin);
        make.size.mas_equalTo(CGSizeMake(50, 30));
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = kColorBackground;
    [self addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(titleLabel.mas_bottom).offset(margin *2 - 5);
        make.height.offset(1);
    }];
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.textColor = kColorText;
    leftLabel.font = [UIFont systemFontOfSize:14];
    leftLabel.text = @"兑换数额";
    [self addSubview:leftLabel];
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(margin *2);
        make.top.equalTo(lineView.mas_bottom).offset(margin*2);
        make.width.offset(60);
    }];
    
    UIButton *actionBtn = [[UIButton alloc] init];
    actionBtn.tag = 1000;
    [actionBtn setTitle:@"全部兑换" forState:(UIControlStateNormal)];
    [actionBtn addTarget:self action:@selector(actionCilk:) forControlEvents:(UIControlEventTouchUpInside)];
    [actionBtn setTitleColor:kColorWithFloat(0x09a570) forState:(UIControlStateNormal)];
    actionBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:actionBtn];
    
    [actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-margin * 2);
        make.centerY.equalTo(leftLabel);
        make.width.offset(60);
    }];
    
    jfTextField = [[UITextField alloc] init];
    jfTextField.placeholder = @"请输入红积分数额";
    jfTextField.font = [UIFont systemFontOfSize:14];
    jfTextField.keyboardType = UIKeyboardTypeDecimalPad;
    [self addSubview:jfTextField];
    
    [jfTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftLabel.mas_right).offset(margin);
        make.right.equalTo(actionBtn.mas_left).offset(-margin);
        make.centerY.height.equalTo(actionBtn);
    }];
    
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = kColorText;
    [self addSubview:bottomLineView];
    
    [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(jfTextField.mas_bottom);
        make.left.right.equalTo(jfTextField);
        make.height.offset(1);
    }];
    
    UILabel *bottomLabel = [[UILabel alloc] init];
    bottomLabel.text = @"(手续费：扣除红积分5%)";
    bottomLabel.textColor = kColorText;
    bottomLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:bottomLabel];

    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomLineView.mas_bottom).offset(margin);
        make.centerX.equalTo(bottomLineView);
    }];
    
    UIButton *senderButton = [[UIButton alloc] init];
    senderButton.tag = 1001;
    senderButton.backgroundColor = [UIColor redColor];
    senderButton.layer.cornerRadius = 15;
    senderButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [senderButton addTarget:self action:@selector(actionCilk:) forControlEvents:(UIControlEventTouchUpInside)];
    [senderButton setTitle:@"确认兑换" forState:(UIControlStateNormal)];
    [self addSubview:senderButton];
    
    [senderButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.centerX.equalTo(self);
        make.bottom.offset(-margin * 2);
    }];
}

- (void)actionCilk:(UIButton *)sender {
    
    if (sender.tag == 1000) {
        jfTextField.text = @(_soce).description;
        return;
    }
    
    if (_callBack) {
        _callBack([jfTextField.text integerValue]);
    }
    
}

- (void)closeButtonAction:(UIButton *)sender{
    
    if (self.closeBlock) self.closeBlock();
}

@end
